package com.paw.kafka.elk.service;

import java.util.concurrent.atomic.AtomicInteger;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lvzihai
 * @date 2021/7/27
 **/
@Service
public class MetricService {

  @Autowired
  private MeterRegistry meterRegistry;

  /**
   * 创建Counter类型metric
   * @param name metric名称
   * @param tags 标签 key/value形式
   * @return
   */
  public Counter registerCounterMetric (String name, String... tags){
    return Counter.builder(name).tags(tags).register(meterRegistry);
  }

  /**
   * 创建Gauge类型metric
   * @param name metric名称
   * @param tags 标签 key/value形式
   * @return
   */
  public AtomicInteger registerGaugeMetric (String name, AtomicInteger atomicInteger,String... tags){
    Gauge.builder(name, atomicInteger, AtomicInteger::get).tags(tags).register(meterRegistry);
    return atomicInteger;
  }

  /**
   * 创建Histogram类型metric
   * @param name metric名称
   * @param bucket 桶
   * @param tags 标签 key/value形式
   * @return
   */
  public DistributionSummary registerHistogramMetric (String name, long[] bucket, String... tags){
    return DistributionSummary.builder(name).tags(tags).sla(bucket).register(meterRegistry);
  }

  /**
   * 创建Summary类型metric
   * @param name metric名称
   * @param percentiles 百分位
   * @param tags 标签 key/value形式
   * @return
   */
  public DistributionSummary registerSummaryMetric (String name, double[] percentiles, String... tags){
    return DistributionSummary.builder(name).tags(tags).publishPercentiles(percentiles).register(meterRegistry);
  }

}
